import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

class VideoPlayerPager extends StatefulWidget {
  final String videoUrl;
  const VideoPlayerPager(this.videoUrl);
  @override
  _VideoPlayerPagerState createState() => _VideoPlayerPagerState(videoUrl);
}

class _VideoPlayerPagerState extends State<VideoPlayerPager> {
  VideoPlayerController _controller;
  final String videoUrl;
  _VideoPlayerPagerState(this.videoUrl);

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(videoUrl)
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {
          _controller.value.isPlaying
              ? _controller.pause()
              : _controller.play();
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme:new ThemeData.dark(),
      home: Scaffold(
        body: Center(
          child: _controller.value.initialized
              ? AspectRatio(
            aspectRatio: _controller.value.aspectRatio,
            child: VideoPlayer(_controller),
          )
              : Container(),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}